import Vec2 = cc.Vec2;
import HeroContact, {colliderGroup} from "./HeroContact";
import GameData_red_blue_man, {emitType, specialType} from "./GameData_red_blue_man";
import {colorType} from "./HeroControl";
import Music, {musicName} from "../PublicScript/Music";
import GameControl_red_blue_man from "./GameControl_red_blue_man";
import PlaySpecialMusic from "./PlaySpecialMusic";

const {ccclass, property} = cc._decorator;

@ccclass
export default class One_Side_Platform extends cc.Component {

    @property({tooltip: "是否开启移动"})
    move_side: boolean = false;
    @property({tooltip: "移动的距离"})
    move_distances: Vec2 = cc.v2(0,0);
    @property({tooltip:"速度"})
    speed: number = 300;

    downMove = false;
    initPos = cc.v2(0,0);
    nowPos = cc.v2(0,0);
    endPos = cc.v2(0,0);

    onLoad () {
        cc.game.on(emitType.changeMoveSide, this.changeMoveSide, this);
    }

    start () {
        this.init();
    }

    //移动
    move() {
        let time = this.move_distances.mag() / this.speed * 2.5;
        this.node.runAction(
            cc.repeatForever(
                cc.sequence(
                    cc.moveTo(time, this.endPos),
                    cc.moveTo(time, this.initPos)
                )
            )
        );
    }

    init(){
        if(this.speed == 0){
            this.speed = 300
        }
        this.node.getPosition(this.initPos);
        this.endPos = cc.v2(this.initPos.x + this.move_distances.x, this.initPos.y + this.move_distances.y);
        if(this.move_side){
            this.move();
            let nodeWidthMax = this.node.parent.x + this.node.width/2;
            let nodeWidthMin = this.node.parent.x - this.node.width/2;
            let nodeHeightMax = this.node.parent.y + this.node.height/2;
            let nodeHeightMin = this.node.parent.y - this.node.height/2;

            GameData_red_blue_man.moveSideStateList.push(false);
            let tag = GameData_red_blue_man.moveSideStateList.length - 1;

            console.log("===========移动");
            let special = this.node.addComponent(PlaySpecialMusic);
            special.setValue({name: musicName.Mechanical_sound, widthMax: nodeWidthMax, widthMin: nodeWidthMin, HeightMax: nodeHeightMax, HeightMin: nodeHeightMin, tag: tag});
        }
    }

    moveEvent(dir){
        this.node.stopAllActions();
        let pos = dir == 1 ? this.endPos : this.initPos;
        let nowPos = this.node.getPosition();
        let dis = pos.sub(nowPos).mag();
        let time = dis / this.speed;
        this.node.runAction(
            cc.moveTo(time, pos),
        );
    }

    //拿到英雄的节点
    heroRigidbody = null;
    onMoveSide = {};

    changeMoveSide(type){
        this.onMoveSide[type] = false;
    }

    onBeginContact(contact, self, other) {
        //改变摩擦力
        if(other.tag == 0 && other.node.group == colliderGroup.hero && ((!this.onMoveSide[colorType.red] && other.node.getComponent(HeroContact).manType == colorType.red) || (!this.onMoveSide[colorType.blue] && other.node.getComponent(HeroContact).manType == colorType.blue))){
            other.node.getComponent(HeroContact).setOnMoveSide(true);
            this.onMoveSide[other.node.getComponent(HeroContact).manType] = true;
            other.friction = 1;
            other.apply();
        }

        //获得世界坐标系下的信息
        let worldManifold = contact.getWorldManifold();
        //碰撞点(可能会有多个)
        let points = worldManifold.points;
        //法向量
        let normal = worldManifold.normal;
        //获取所有点的碰撞点的发生的方向
        for(let i in points){
            //获取在世界坐标系下的刚体线速度
            let selfVel = cc.v2(0,0);
            let otherVel = cc.v2(0,0);
            self.body.getLinearVelocityFromWorldPoint(worldManifold.points[i], selfVel);
            other.body.getLinearVelocityFromWorldPoint(worldManifold.points[i], otherVel);
            //获取他们之间的相对速度
            let relativeVelocity = selfVel.sub(otherVel);
            if(relativeVelocity.y > -32){
                return;
            }
            contact.disabled = true;
        }

    }

    update (dt) {

    }
}
